package org.lq.dlz.query;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.lq.dlz.util.DateUtils;

import lombok.Data;
import lombok.extern.log4j.Log4j;
@Data
@Log4j
public class ProductQuery {
	private String cid;
	private String name;
	private Date createTime;
	private String clomes;
	
	
	public String toWhereSQL() {
		String sql = " where 1=1 ";
		if(StringUtils.isNoneBlank(name)) {
			sql +="and name like '%"+name+"%' ";
		}
		if(createTime!=null) {
			sql +=" and create_time like '"+DateUtils.dateTime(createTime)+"%' ";
		}
		if(StringUtils.isNoneBlank(cid)) {
			sql +=" and category_id = "+cid+" ";
		}
		
		log.info("ProductQuery SQL : "+sql);
		return sql;
	}
	
	public String toORderSQL() {
		String sql ="order by ";
		if(StringUtils.isNoneBlank(clomes)) {
			if(StringUtils.isNoneBlank(clomes)) {
				String[] arr = clomes.split(",");
				for (String c : arr) {
					sql+=" "+c+" asc ,";
				}
				
				sql= sql.substring(0,sql.length()-1);
			}
			log.info("Order SQL : "+sql);
			
		}else {
			sql+=" price desc , update_time desc";
		}
		
		
		return sql;
	}
	
	
	
}
